import time
import api, gui, IAccessibleHandler, mouseHandler, tones, ui, winUser, wx
from controlTypes.role import Role
timer = None
msgList = None
lastMsg = None
def init():
    # 初始化
    global msgList, timer
    timer = wx.Timer(gui.mainFrame)
    gui.mainFrame.Bind(wx.EVT_TIMER, onTimerEvent, timer)
    reset()
def release ():
    global msgList, timer
    # 释放资源
    if not timer: return
    timer.Stop()
    timer, msgList = None, None
def reset():
    global msgList
    # 用于快捷键朗读消息
    msgList = None
def switchAutoSpeakMsg ():
    # 自动朗读消息开关
    if not timer: return
    tones.beep(800, 50)
    if timer.IsRunning ():
        timer.Stop ()
        ui.message ('已关闭消息自动朗读')
        return
    # 开始自动朗读
    timer.Start (1000)
    ui.message ('已开启消息自动朗读')
def onTimerEvent(event):
    global lastMsg
    # 判断当前窗口是不是 qq
    obj = api.getForegroundObject()
    if obj.appModule.appName != 'qq' or obj.extendedWindowStyle != 525056: return
    # 处理消息自动朗读
    msg = getLastMsg ()
    if lastMsg == msg:return
    lastMsg = msg
    ui.message(msg)
def getMsgList():
    global msgList
    # 获取聊天窗口消息列表，有缓存
    if msgList:
        return msgList
    obj = api.getForegroundObject()
    # 没有获取到窗口对象就直接返回
    if not obj: return
    # 找到消息列表
    for child in obj.recursiveDescendants:
        if child.role != Role.LIST or child.name != '消息': continue
        msgList = child.location
        return msgList
def getLastMsg ():
    # 获取最后一条消息
    msgList = getMsgList()
    # 如果没有获取到消息列表就直接返回
    if not msgList: return
    # 找到最后一条消息
    l, t, w, h = msgList
    x = int(l + w / 2)
    y = t + h
    item, index = IAccessibleHandler.accessibleObjectFromPoint(x, y)
    name, role = None, None
    while t < y:
        if item:
            name, role = item.accName(index), item.accRole(index)
            if name and role == 34:break
        y -= 10
        item, index = IAccessibleHandler.accessibleObjectFromPoint(x, y)
    return name
def clickButton(name, onChoice = lambda x: True):
    # 点击按钮
    obj = api.getForegroundObject()
    if not obj:
        # 没有获取到窗口对象就直接返回
        tones.beep(500, 50)
        return
    tones.beep(800, 50)
    # 找到按钮
    for child in obj.recursiveDescendants:
        if child.role == Role.BUTTON and child.description == name and onChoice(child):
            click(child)
def hasObject(name):
    # 判断是否有某个对象
    obj = api.getForegroundObject()
    if not obj: return
    for child in obj.recursiveDescendants:
        if child.name and name in child.name or child.description and name in child.description:
            return True
def expectPopupMenu (callback, timeout = 5):
    # 等待一个弹出菜单
    ts = time.time ()
    def cb ():
        if ts + timeout < time.time (): return
        focus = api.getFocusObject ()
        if focus.name == 'TXMenuWindow':
            wx.CallLater(0.5, callback)
            return
        wx.CallLater (0.1, cb)
    cb ()
def clickMenu (name):
    # 点击菜单
    obj = api.getFocusObject()
    if not obj:
        # 没有获取到焦点对象就直接返回
        tones.beep(500, 50)
        return
    tones.beep(800, 50)
    # 找到菜单项
    for child in obj.recursiveDescendants:
        if child.role == Role.MENUITEM and child.name == name:
            click(child)
def click(obj):
    # 点击操作
    l, t, w, h = obj.location
    x, y = int(l + w / 2), int(t + h / 2)
    winUser.setCursorPos(x, y)
    mouseHandler.executeMouseMoveEvent(x, y)
    mouseHandler.doPrimaryClick()